package com.example.f8399.testdemo;

import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.example.f8399.testdemo.databinding.ActivityDownload2Binding;
import com.example.f8399.testdemo.fragment.DownCachingFragment;
import com.example.f8399.testdemo.fragment.DownCompleteFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 下载展示
 * */
public class DownloadActivity2 extends BaseActivity implements DownCompleteFragment.OnCompleteListener,DownCachingFragment.OnCachingListener{

    ActivityDownload2Binding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_download2);

        initView();
    }

    private void initView() {
        List<Fragment> fragments = new ArrayList<>();
        DownCompleteFragment fragment1 = DownCompleteFragment.newInstance("1", "2");
        DownCachingFragment fragment2 = DownCachingFragment.newInstance("1", "2");
        fragments.add(fragment1);
        fragments.add(fragment2);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        mBinding.viewPager.setAdapter(new MyAdapter(supportFragmentManager,fragments));
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        addTabDivider(mBinding.tabLayout);
        setTabLine(mBinding.tabLayout,20,20);

    }

    /**
     * 给tablayout添加分隔符
     * @param tabLayout
     * @param
     */
    private void addTabDivider(TabLayout tabLayout) {
        //我也很疑惑为什么不用for循环而是直接指定index 0 就可以了
        //参考1：https://blog.csdn.net/qq_26413249/article/details/54093288
        //参考2：https://blog.csdn.net/wlxfxy/article/details/73732574
        LinearLayout childAt = (LinearLayout) tabLayout.getChildAt(0);
        childAt.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        childAt.setDividerDrawable(ContextCompat.getDrawable(this,R.drawable.divider_1dp));
    }


    /**
     * @param who 1:完成 2:下载中
     * @param uri
     */
    @Override
    public void onFragmentInteraction(int who,Uri uri) {
        switch (who) {
            case 1://已完成

                break;
            case 2://下载中

                break;

        }
    }

    class MyAdapter extends FragmentPagerAdapter {
        String titles[] = {"已完成","下载中"};
        List<Fragment> mList = new ArrayList<>();
        public MyAdapter(FragmentManager fm,List<Fragment> plist) {
            super(fm);
            mList = plist;
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    /**
     * 给tablayout的底部标签设置长短
     * @param tab
     * @param left
     * @param right
     */
    public void setTabLine(TabLayout tab, int left, int right) {
        try {
            Class<?> tablayout = tab.getClass();
            Field tabStrip = tablayout.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout ll_tab = (LinearLayout) tabStrip.get(tab);
            for (int i = 0; i < ll_tab.getChildCount(); i++) {
                View child = ll_tab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                //修改两个tab的间距
                params.setMarginStart(SizeUtils.dp2px(left));
                params.setMarginEnd(SizeUtils.dp2px(right));
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            LogUtils.e(e.toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            LogUtils.e(e.toString());

        }
    }
}
